package com.blog.blog.service.impl;

import com.blog.blog.service.I18nService;
import lombok.RequiredArgsConstructor;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 国际化服务实现
 */
@Service
@RequiredArgsConstructor
public class I18nServiceImpl implements I18nService {
    
    private final MessageSource messageSource;
    
    @Override
    public String getMessage(String key, Object[] args, Locale locale) {
        try {
            return messageSource.getMessage(key, args, locale);
        } catch (Exception e) {
            return key; // 如果找不到消息，返回key本身
        }
    }
    
    @Override
    public String getMessage(String key, Object... args) {
        return getMessage(key, args, LocaleContextHolder.getLocale());
    }
    
    @Override
    public Map<String, String> getSupportedLanguages() {
        Map<String, String> languages = new LinkedHashMap<>();
        languages.put("zh_CN", "简体中文");
        languages.put("en_US", "English");
        languages.put("ja_JP", "日本語");
        return languages;
    }
    
    @Override
    public Map<String, String> getAllMessages(Locale locale) {
        Map<String, String> messages = new HashMap<>();
        
        // 定义所有消息键
        String[] messageKeys = {
            // 通用消息
            "common.success", "common.error", "common.warning", "common.info",
            "common.confirm", "common.cancel", "common.save", "common.delete",
            "common.edit", "common.add", "common.search", "common.reset",
            "common.submit", "common.back", "common.next", "common.previous",
            "common.loading", "common.noData", "common.total", "common.page",
            
            // 用户相关
            "user.login", "user.logout", "user.register", "user.profile",
            "user.username", "user.password", "user.email", "user.nickname",
            "user.avatar", "user.loginSuccess", "user.loginFailed",
            "user.registerSuccess", "user.registerFailed",
            
            // 文章相关
            "article.title", "article.content", "article.summary", "article.category",
            "article.tags", "article.author", "article.publishTime", "article.viewCount",
            "article.likeCount", "article.commentCount", "article.publish",
            "article.draft", "article.top", "article.list", "article.detail",
            "article.search", "article.noArticles",
            
            // 评论相关
            "comment.content", "comment.author", "comment.time", "comment.reply",
            "comment.like", "comment.submit", "comment.placeholder",
            "comment.noComments", "comment.success", "comment.failed",
            
            // 分类相关
            "category.name", "category.description", "category.list",
            "category.manage", "category.add", "category.edit", "category.delete",
            
            // 标签相关
            "tag.name", "tag.color", "tag.list", "tag.manage",
            "tag.add", "tag.edit", "tag.delete",
            
            // 简历相关
            "resume.title", "resume.summary", "resume.skills", "resume.experience",
            "resume.education", "resume.projects", "resume.contact",
            "resume.download", "resume.view",
            
            // 管理相关
            "admin.dashboard", "admin.users", "admin.articles", "admin.categories",
            "admin.tags", "admin.comments", "admin.settings", "admin.logs",
            "admin.backup", "admin.monitor", "admin.permissions", "admin.roles",
            
            // 系统相关
            "system.title", "system.version", "system.status", "system.health",
            "system.performance", "system.backup", "system.restore",
            "system.monitor", "system.alert",
            
            // 错误消息
            "error.404", "error.500", "error.403", "error.400",
            "error.network", "error.timeout", "error.unknown",
            
            // 验证消息
            "validation.required", "validation.email", "validation.minLength",
            "validation.maxLength", "validation.pattern"
        };
        
        for (String key : messageKeys) {
            messages.put(key, getMessage(key, null, locale));
        }
        
        return messages;
    }
    
    @Override
    public Map<String, String> getAllMessages() {
        return getAllMessages(LocaleContextHolder.getLocale());
    }
}
